package twosigma;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Two Sigma code test problem 2 solution
 * 
 * @author yunzhang
 * 
 */
public class Databackup {

	private Map<String, Set<String>> dataLocMap;

	private Integer dsNum;

	public Databackup() {
		dataLocMap = new HashMap<String, Set<String>>();
		dsNum = 0;
	}

	/**
	 * Build inverse index that for each data, store the collection of data
	 * center has it in a set
	 */
	public void buildInverseMap() {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		try {
			String s;
			if ((s = in.readLine()) != null && s.length() != 0)
				dsNum = Integer.valueOf(s);

			if (dsNum != 0) {
				// Build inverse index
				for (int curDS = 1; curDS <= dsNum; ++curDS) {
					s = in.readLine();

					if (s != null && !s.isEmpty()) {
						String[] allData = s.split(" ");

						for (String dataId : allData) {
							if (dataLocMap.containsKey(dataId)) {
								dataLocMap.get(dataId).add(
										((Integer) curDS).toString());
							} else {
								Set<String> set = new HashSet<String>();
								set.add(((Integer) curDS).toString());
								dataLocMap.put(dataId, set);
							}
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	public void printBackupSequence() {
		for (String did : dataLocMap.keySet()) {
			Set<String> locs = dataLocMap.get(did);

			for (int i = 1; i <= dsNum; ++i) {
				String dsId = ((Integer) i).toString();
				if (!locs.contains(dsId))
					System.out.println(did + " " + pickupRandom(locs) + " "
							+ dsId);
			}
		}
		System.out.println("done");
	}

	private String pickupRandom(Set<String> set) {
		return (String) set.toArray()[0];
	}

	public static void main(String[] args) {
		Databackup databackup = new Databackup();
		databackup.buildInverseMap();
		databackup.printBackupSequence();
	}

}
